package demoMod.invincibleOne.patches;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.evacipated.cardcrawl.modthespire.lib.*;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.helpers.SaveHelper;
import com.megacrit.cardcrawl.metrics.Metrics;
import com.megacrit.cardcrawl.monsters.MonsterGroup;
import com.megacrit.cardcrawl.rooms.TrueVictoryRoom;
import com.megacrit.cardcrawl.saveAndContinue.SaveAndContinue;
import com.megacrit.cardcrawl.screens.DeathScreen;
import com.megacrit.cardcrawl.screens.GameOverScreen;
import com.megacrit.cardcrawl.screens.VictoryScreen;
import com.megacrit.cardcrawl.vfx.AbstractGameEffect;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.characters.InvincibleOne;
import demoMod.invincibleOne.effects.EndCGEffect;
import demoMod.invincibleOne.relics.SecondWindRelic;
import demoMod.invincibleOne.relics.TaskGoal;
import com.megacrit.cardcrawl.core.Settings;

import java.util.Dictionary;
import java.util.HashSet;

import static com.megacrit.cardcrawl.core.CardCrawlGame.languagePack;

// 碎心算输
public class TrueVictoryScreenPatch {
    @SpirePatch(
            clz = VictoryScreen.class,
            method = SpirePatch.CONSTRUCTOR
    )
    public static class PatchConstructor {
        private static final String DESCRIPTION = languagePack.getUIString(InvincibleOneMod.makeID("HeartDead")).TEXT[0];
        @SpireInsertPatch(rloc = 94 - 64)
        public static SpireReturn<Void> Insert(GameOverScreen screen, MonsterGroup group) {
            if (AbstractDungeon.player instanceof InvincibleOne) {
                TaskGoal tg = (TaskGoal) AbstractDungeon.player.getRelic(TaskGoal.ID);
                if (tg.heartDead) {
                    AbstractDungeon.is_victory = false;
                    for (AbstractCard c : AbstractDungeon.player.hand.group)
                        c.unhover();
                    AbstractDungeon.dynamicBanner.appear(DESCRIPTION);
                    Metrics metrics = new Metrics();
                    metrics.gatherAllDataAndSave(true, true, null);
                    if (SaveHelper.shouldDeleteSave())
                        SaveAndContinue.deleteSave(AbstractDungeon.player);
                    CardCrawlGame.playerPref.flush();
                    return SpireReturn.Return();
                }
            }
            return SpireReturn.Continue();
        }
    }

    @SpirePatch(
            clz = VictoryScreen.class,
            method = "render"
    )
    public static class DeathCGPatch {
        public static VictoryScreen victoryScreen;
        public static AbstractGameEffect effect;
        @SpireInsertPatch(rloc = 1)
        public static void Insert(VictoryScreen _instance, SpriteBatch sb) {
            if (AbstractDungeon.player instanceof InvincibleOne) {
                TaskGoal tg = (TaskGoal) AbstractDungeon.player.getRelic(TaskGoal.ID);
                if (!tg.heartDead) {
                    if (victoryScreen != _instance && effect == null) {
                        victoryScreen = _instance;
                        effect = new EndCGEffect();
                        Settings.hideTopBar = true;
                    }
                    if (effect != null) {
                        effect.update();
                        if (effect.isDone) {
                            effect = null;
                            Settings.hideTopBar = false;
                            return;
                        }
                        effect.render(sb);
                    }
                }
            }
        }
    }
}
